package adapletplugin.editors;

import objects.IObject;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;

public abstract class AbstractSubEditor
{
	protected ScrolledComposite fComposite;
	protected FormToolkit fToolkit;
	protected IObject fFocusObject;
	protected Composite fSubComposite;

	protected MapFileEditor fEditor;

	public AbstractSubEditor(ScrolledComposite composite, FormToolkit toolkit, MapFileEditor parentEditor)
	{
		fComposite = composite;
		fToolkit = toolkit;
		fEditor = parentEditor;

		initCreate();

		create();

		fComposite.setMinSize(fSubComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));

		fSubComposite.setEnabled(false);
	}

	protected abstract void create();

	public final void setParent(Composite parent)
	{
		fComposite.setParent(parent);
	}

	public final void updateData(IObject focusObject)
	{
		fFocusObject = focusObject;

		fSubComposite.setEnabled(fFocusObject != null);

		updateFields();
	}

	protected abstract void updateFields();

	protected final void initCreate()
	{
		fComposite.setLayout(new GridLayout(1, true));
		fComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		fComposite.setExpandHorizontal(true);
		fComposite.setExpandVertical(true);

		fSubComposite = fToolkit.createComposite(fComposite);
		fSubComposite.setLayout(new GridLayout(1, true));
		fSubComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		fComposite.setContent(fSubComposite);
	}

	public final void refresh()
	{
		fEditor.refresh();
	}

}
